﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ControleRepresentanteSync.Application.Wizard
{
    class WizardPageProvider : IWizardPageProvider
    {
        private readonly List<IWizardPageViewModel> m_Pages;
        private int m_Index;

        public WizardPageProvider(params IWizardPageViewModel[] pages)
        {
            m_Pages = pages.ToList();
            m_Index = 0;
        }

        public IWizardPageViewModel Current { get { return m_Pages[m_Index]; } }

        public bool CanGoBack { get { return m_Index > 0; } }

        public bool IsFinish { get { return m_Index == m_Pages.Count - 1; } }

        public IWizardPageViewModel MoveNext()
        {
            return m_Pages[++m_Index];
        }

        public IWizardPageViewModel MoveBack()
        {
            return m_Pages[--m_Index];
        }

        public event EventHandler Changed;

        public void AddPage(IWizardPageViewModel page)
        {
            m_Pages.Add(page);
            FireChanged();
        }

        public void RemovePage(IWizardPageViewModel page)
        {
            m_Pages.Remove(page);
            FireChanged();
        }

        private void FireChanged()
        {
            if (Changed != null)
                Changed(this, EventArgs.Empty);
        }

        public IEnumerable<IWizardPageViewModel> Pages { get { return m_Pages; } }
        
        public void Dispose()
        {
            foreach (IWizardPageViewModel wizardPageViewModel in m_Pages)
            {
                wizardPageViewModel.FireOnClose();
            }
        }
    }
}